package com.plpm.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.plpm.service.PLPMService;

import lombok.extern.slf4j.Slf4j;

/**
 * PLPM服务控制器
 *
 * @author PLPM Team
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/plpm")
public class PLPMController {

    @Autowired
    private PLPMService plpmService;

    /**
     * 获取PLPM服务状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatus() {
        Map<String, Object> response = new HashMap<>();

        try {
            // 这里可以添加更多的状态检查逻辑
            response.put("success", true);
            response.put("status", "RUNNING");
            response.put("message", "PLPM服务运行正常");
            response.put("timestamp", System.currentTimeMillis());

        } catch (Exception e) {
            log.error("Failed to get PLPM status", e);
            response.put("success", false);
            response.put("status", "ERROR");
            response.put("message", "获取PLPM状态失败: " + e.getMessage());
        }

        return ResponseEntity.ok(response);
    }

    /**
     * 手动触发PLPM文件下载任务（测试接口）
     */
    @GetMapping("/test/download-task")
    public ResponseEntity<Map<String, Object>> testDownloadTask() {
        log.info("手动触发PLPM文件下载任务测试");
        
        try {
            Map<String, Object> result = plpmService.executeDownloadTask();
            result.put("timestamp", System.currentTimeMillis());
            result.put("triggerType", "MANUAL_TEST");
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("手动触发PLPM文件下载任务失败", e);
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "手动触发任务失败: " + e.getMessage());
            errorResponse.put("timestamp", System.currentTimeMillis());
            errorResponse.put("triggerType", "MANUAL_TEST");
            
            return ResponseEntity.status(500).body(errorResponse);
        }
    }
  
}
